import 'package:flutter/material.dart';

class OverlaySlider extends StatefulWidget {
  final void Function(double)? onChange;

  const OverlaySlider({super.key, this.onChange});

  @override
  State<OverlaySlider> createState() => _OverlaySliderState();
}

class _OverlaySliderState extends State<OverlaySlider> {
  final OverlayPortalController _controller = OverlayPortalController();

  double _value = 0.4;

  @override
  Widget build(BuildContext context) {
    return OverlayPortal(
        controller: _controller,
        overlayChildBuilder: (BuildContext context) {
          return Positioned(
              right: 50,
              bottom: 50,
              child: Card(
                  child: Slider(
                      value: _value, min: 0, max: 1, onChanged: _onChanged)));
        },
        child: IconButton(
            onPressed: () {
              _controller.toggle();
            },
            icon: Icon(Icons.volume_mute)));
  }

  void _onChanged(double value) {
    setState(() {
      _value = value;
    });
    widget.onChange?.call(value);
  }
}
